package com.vp.org.dao.impl;

import com.vp.org.constant.Sql;
import com.vp.org.dao.RemarkDao;
import com.vp.org.pojo.BookInfo;
import com.vp.org.pojo.Remark;
import com.vp.org.pojo.UserInfo;
import com.vp.org.util.JdbcUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;
import java.util.Map;

public class RemarkDaoImpl extends JdbcUtil implements Sql, RemarkDao {

    // 后台添加评论
    // insert into remark values (default,1,1,'真是好书','解答之书');
    @Override
    public int addRemark(UserInfo userInfo, BookInfo bookInfo, Remark remark) {
        return template.update(REMARK_ADD,userInfo.getUserId(),bookInfo.getId(),
                remark.getContent(),remark.getRank());
    }

    // 删除评论
    // delete from remark where id = ? and user_id = ?;
    @Override
    public int deleteRemark(UserInfo userInfo, Remark remark) {
        return template.update(REMARK_DELETE,remark.getId(),userInfo.getUserId());
    }

    // 总评论数
    // select count('id') count from remark;
    @Override
    public long remarkCount() {
        return (long)template.queryForMap(REMARK_COUNT).get("count");
    }

    // 分多少页
    @Override
    public int remarkPages() {
            int totalSize = (int)remarkCount();
            return totalSize % 6 == 0 ? totalSize / 6 : totalSize / 6 + 1;

    }

    // 后台分页查询评论
    // select user_info.username,user_info.phone,remark.* from user_info,remark where user_info.user_id in(select user_id from remark where book_id in(select id from book_info where admin='admin')) limit 0,10;
    // 更改后 select u.username,u.phone,r.* from user_info u,remark r where u.user_id = r.user_id limit 0, 6;
    @Override
    public List<Map<String, Object>> queryPageRemark(int page) {

//        return template.queryForList(REMARK_QUERY, adminInfo.getAdmin(), (page - 1) * 6);
        return template.queryForList(REMARK_QUERY,(page - 1)*6);
    }

    @Override
    public List<Remark> queryAll(int id) {
        return template.query(ALL_REMARK, new BeanPropertyRowMapper<>(Remark.class), id);
    }

    @Override
    public int delete(int id) {
        return template.update(DELETE, id);
    }
}
